using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FireCracker : Trap {

    public float timeBoom;
    public float distanceBoom;
    public TextMesh textMesh;
    public GameObject explosion;

    private LevelManager _levelMgr;

	// Use this for initialization
	void Start () {
        _levelMgr = FindObjectOfType(typeof(LevelManager)) as LevelManager;

        typeTrap = Const.TRAP_TYPE.FIRECRACKER;

        textMesh.renderer.material.color = Color.black;

        this.audio.Play();
        this.audio.loop = true;
	}
	
	// Update is called once per frame
	void Update () {

        timeBoom -= Time.deltaTime;
        textMesh.text = Mathf.Ceil(timeBoom).ToString();

        if(timeBoom < 0)
        {
            Explode();
        }

	}

    //Explosion
    void Explode()
    {
        SoundManager.Instance.PlayFX("fireCrackerExplode");

        List<Enemy> enemys = _levelMgr.GetEnemyAtDistance(this.transform.position, distanceBoom);

        foreach (Enemy enemy in enemys)
        {
            enemy.FireCrackerEffect();
        }

        explosion.renderer.enabled = true;
        //explosion.GetComponent<Anim>().resetTimer(explosion.renderer.material.GetTexture("_MainTex"), 3);
        explosion.gameObject.active = true;

        textMesh.renderer.enabled = false;
        this.transform.renderer.enabled = false;

        StartCoroutine(WaitForDeleteExplosion());
    }

    IEnumerator WaitForDeleteExplosion()
    {
        yield return new WaitForSeconds(0.15f);

        this.gameObject.active = false;
        Destroy(this.gameObject);

    }
}
